package com.example.myapplication;

import android.util.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LectureSpider {
    private static final String TAG = "LectureSpider";
    private static final String BASE_URL = "https://www.swufe.edu.cn/xsjz.htm";
    private static final int TIMEOUT_MILLIS = 15000;
    private static final int MAX_RETRIES = 3;

    public List<Map<String, String>> fetchLectures() {
        List<Map<String, String>> lectures = new ArrayList<>();
        int retryCount = 0;

        while (retryCount < MAX_RETRIES) {
            try {
                Log.d(TAG, "尝试获取讲座信息，第" + (retryCount + 1) + "次尝试");

                // 获取网页内容
                Document doc = Jsoup.connect(BASE_URL)
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                        .timeout(TIMEOUT_MILLIS)
                        .get();

                Log.d(TAG, "成功连接到网页");

                // 根据提供的路径选择讲座列表
                Elements items = doc.select("html body div.wrapper div.c_main div.sy_bg div.c_box div.c_box_left ul.whitenewslist.clearfix li");

                if (items.isEmpty()) {
                    Log.w(TAG, "未找到讲座列表，尝试备用选择器");
                    items = doc.select("ul.whitenewslist li"); // 简化选择器
                }

                Log.d(TAG, "找到 " + items.size() + " 个讲座项");

                for (Element item : items) {
                    try {
                        Map<String, String> lecture = new HashMap<>();

                        // 提取标题和链接
                        Element titleElement = item.select("a").first();
                        String title = titleElement != null ? titleElement.text().trim() : "";

                        // 提取时间 - 根据实际页面结构调整
                        Element timeElement = item.select("span").first(); // 可能需要更精确的选择器
                        String time = timeElement != null ? timeElement.text().trim() : "";

                        // 处理链接
                        String link = titleElement != null ? titleElement.attr("href") : "";
                        if (!link.isEmpty() && !link.startsWith("http")) {
                            link = link.startsWith("/") ? "https://www.swufe.edu.cn" + link
                                    : "https://www.swufe.edu.cn/" + link;
                        }

                        if (!title.isEmpty()) {
                            lecture.put("title", title);
                            lecture.put("time", time);
                            lecture.put("link", link);
                            lectures.add(lecture);

                            Log.d(TAG, "添加讲座: " + title +
                                    " | 时间: " + time +
                                    " | 链接: " + link);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "处理单个讲座项时出错: " + e.getMessage());
                        continue;
                    }
                }

                if (!lectures.isEmpty()) {
                    Log.d(TAG, "成功获取到 " + lectures.size() + " 个讲座信息");
                    break;
                }

                retryCount++;
                if (retryCount < MAX_RETRIES) {
                    Thread.sleep(2000);
                }

            } catch (Exception e) {
                Log.e(TAG, "获取讲座信息失败: " + e.getMessage());
                retryCount++;
                if (retryCount < MAX_RETRIES) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
            }
        }

        if (lectures.isEmpty()) {
            Log.e(TAG, "在" + MAX_RETRIES + "次尝试后仍未获取到任何讲座信息");
        }

        return lectures;
    }
}